Covert dateandtime

Hello,

RPE doesn't support dates with time so I need to format the current dateandtime to a string below is the function I wrote is there a better solution?

 

string ConvertDateandTime()
{
    Date dateValue = dateAndTime(today)
    Date d = dateOnly(dateValue)
    Locale loc = userLocale

    int now = intOf(dateValue)
    int day = intOf(date(stringOf(d)))
    int diff = now-day
    int hours = diff / 3600

    if(hours > 11) {
        return(stringOf(dateValue, loc, "dddd, d MMMM yyyy") " PM")
    } else {
        return(stringOf(dateValue, loc, "dddd, d MMMM yyyy") " AM")
    }
}

print ConvertDateandTime


EHcnck - Mon Sep 16 09:01:23 EDT 2013

Re: Covert dateandtime
llandale - Tue Sep 17 10:10:20 EDT 2013

Not sure what you are trying to do.  In any case:

  • It would be better if the function accepted the "Date" and converted it; instead of presuming "today".
  • Your "PM"/"AM" code doesn't look right.  "20:55:10 PM" doesn't make sense.  Perhaps "20:55:10 (pm)" is slightly better.
  • Perhaps subtract 12 hours for pm.
  • Perhaps dispay "12" instead of hours "0" for hours.  12:59:59 am comes right before 01:00:00 am.
  • Perhaps forget about am/pm.

Not sure the following is much better

  • string ConvertDateandTime(Date in_dat)
  • {
  •     Date dateValue = dateAndTime(in_dat)
  •     Date d = dateOnly(dateValue)
  •     Locale loc = userLocale
  •     int now = intOf(dateValue)
  •     int day = intOf(date(stringOf(d)))
  • //    int day = intOf(d)
  •     int diff = now-day
  •     int hours = diff / 3600
  • //    if(hours > 11) {
  • //        return(stringOf(dateValue, loc, "dddd, d MMMM yyyy") " PM")
  • //    } else {
  • //        return(stringOf(dateValue, loc, "dddd, d MMMM yyyy") " AM")
  • //    }
  •    return(stringOf(dateValue, "yyyy-MM-dd"))
  • }
  • Date dat = dateOf(intOf(today)+(12*3600))
  • print dat "\t\tDefault Format\n"
  • print ConvertDateandTime (dat) "\t\tConverted format\n"

-Louie